<?php
namespace Controller;

use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Container;



class AccountApi extends Base
{

    public function __construct(Container $container)
    {
        parent::__construct($container);

    }

    public function checklogin(Request $req, Response $res)
    {
        $validate = $this->guard->validateToken($req->getParam('tk_name'), $req->getParam('tk_value'));
        if ($validate === true) {
            $username = $req->getParam('username');
            $pwd = $req->getParam('pwd');

            if ($this->checkfrom->regex('require', $username) === false) {
                return $res->withStatus(400)->write('请输入账号');
                exit;
            }
            if ($this->checkfrom->regex('require', $pwd) === false) {
                return $res->withStatus(400)->write('请输入密码');
                exit;
            }

            $hashpwd = hash("sha256", $username . '_' . $pwd);

            $where = [
                'username' => "'$username'",
                'pwd' => "'$hashpwd'",

            ];
            $account = $this->db->where($where)->find('tcp_account');
            if (!empty($account)) {
                $_SESSION['account_id'] = (int)$account['id'];
                $result = $this->db->where(['id' => (int)$account['id']])->update('tcp_account', ['logintime' => time()]);
                if (!empty($result)) {
                    return $res->withStatus(200)->write('登陆成功');
                    exit;
                } else {
                    return $res->withStatus(400)->write('登陆时间修改失败');
                    exit;
                }

            } else {
                return $res->withStatus(400)->write('登陆失败');
                exit;
            }


        } else {
            return $res->withStatus(400)->write('guard error');
            exit;
        }


    }

    public function createaccount(Request $req, Response $res)
    {

        $validate = $this->guard->validateToken($req->getParam('tk_name'), $req->getParam('tk_value'));
        if ($validate === true) {
            $username = $req->getParam('username');
            $pwd = $req->getParam('pwd');

            if ($this->checkfrom->regex('require', $username) === false) {
                return $res->withStatus(400)->write('请输入账号');
                exit;
            }
            if ($this->checkfrom->regex('require', $pwd) === false) {
                return $res->withStatus(400)->write('请输入密码');
                exit;
            }
            $data = [
                'username' => $username,
                'pwd' => hash("sha256", $username . '_' . $pwd),
                'createtime' => time(),
                'logintime' => time(),

            ];


            $count = $this->db->insert('tcp_account', $data);
            if (!empty($count)) {

                return $res->withStatus(200)->write('创建成功');
                exit;
            } else {
                return $res->withStatus(400)->write('创建失败');
                exit;
            }

        } else {
            return $res->withStatus(400)->write('guard error');
            exit;
        }


    }


}